﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoUpdater
{
    public partial class Notify : UserControl
    {

        private string notification = string.Empty;

        public object mutex = new object();

        private bool canShowBalloonTip;

        public Notify(Form owner)
            : this() {
            this.Parent = owner;
        }

        public Notify() {
            InitializeComponent();
            this.notifyIcon.Visible = false;
            this.notifyIcon.Text = String.Empty;
            this.notifyIcon.BalloonTipTitle = Resource.GetString("Notify.Text");
            this.notifyIcon.MouseMove += NotifyIconMouseMove;
            this.notifyIcon.BalloonTipClosed += NotifyIconBalloonTipClosed;
        }



        public Boolean CanShowBalloonTip {
            get {
                lock (mutex) {
                    return canShowBalloonTip;
                }
            }
            set {
                lock (mutex) {
                    this.canShowBalloonTip = value;
                }
            }
        }

        /// <summary>
        /// 通知的文本
        /// </summary>
        public String Notification {
            get { return this.notification; }
            set {
                this.notification = value;
                OnNotification();
            }
        }

        private void OnNotification() {
            if (this.CanShowBalloonTip) {
                this.notifyIcon.BalloonTipText = Notification;
                this.notifyIcon.ShowBalloonTip(5);
            }
        }


        private void NotifyIconBalloonTipClosed(object sender, EventArgs e) {
            CanShowBalloonTip = false;
        }

        private void NotifyIconMouseMove(object sender, MouseEventArgs e) {
            CanShowBalloonTip = true;

        }

        private void NotifyIconDoubleClick(object sender, EventArgs e) {
            var owner = this.Parent as Form;
            this.Visible = false;
            owner.Show();
        }

        private void CloseMenuItemClick(object sender, EventArgs e) {
            this.Parent.Dispose();
            this.Dispose();
            Environment.Exit(0);
        }

        protected override void SetVisibleCore(bool value) {
            base.SetVisibleCore(false);
            this.notifyIcon.Visible = value;
            if (value) {
                this.notifyIcon.BalloonTipText = Notification;
                this.notifyIcon.ShowBalloonTip(5);
            }
        }
    }
}
